import router from "@/router/router";
import { storage } from "./storage";
import { webData } from "./request";
import { state, search, context } from "./store";
import { ref } from "vue";

export const naviTo = async (id: string) => {
    await router.push({
        path: `/document/sleep/${id}`,
    })
}

//验证缓存数据是否存在
const verifyStorageData = (key: string): boolean => {
    return storage.get(key) !== '{}' && storage.get(key).expr > Date.now() ? true : false

}

//页面静态数据
export const StaticData = async () => {
    storage.remove('staticData')
    const verify = verifyStorageData('staticData')
    if (!verify) {
        console.log('获取数据')
        //获取服务器数据
        const staticData = await webData.getStaticData()
        const hotData = await webData.getHotData()


        //保存响应式数据
        // console.log(staticData.data)
        state.naviBar = staticData[3].block[0]
        state.footerBar = staticData[3].block[1]
        state.home = staticData[0]
        state.document = staticData[1]
        state.documentHome = staticData[2]
        state.hotData =  hotData

        //保存数据在缓存
        storage.set('staticData', state)
    } else {
        console.log('缓存数据')
        // console.log(storage.get('staticData'))
        //从缓存获取数据并保存响应式数据
        state.naviBar = storage.get('staticData').value.naviBar
        state.footerBar = storage.get('staticData').value.footerBar
        state.home = storage.get('staticData').value.home
        state.document = storage.get('staticData').value.document
        state.documentHome = storage.get('staticData').value.documentHome
        state.hotData = storage.get('staticData').value.hotData
        // console.log(state)
    }

}

//文档数据更新
export const documentHomeData = (path: string) => {
    webData.getDocumentHomeData(path).then(data => {
        state.documentHome.firstTopics = data.firstTopic
        state.documentHome.secondTopic.topics = data.secondTopic
        state.documentHome.thirdTopic.topics = data.thirdTopic
        state.documentHome.lastTopic = data.lastTopic
        state.documentHome.topView = data.topView
        storage.set('staticData', state)
    })
}

//文档详细更新
export const documentData = (path: string) => {
    webData.getDocumentHomeData(path).then(data => {
        state.topicDoc = data.res[0]
        state.totalNum = data.totalNum
        storage.set('staticData', state)
    })
}


//搜索方法
export const searchMethod = async (input: HTMLInputElement) => {
 
    //聚焦显示
    input.addEventListener('focus', () => {
        search.flag = true
  
    })
    //失焦隐藏
    input.addEventListener('blur', () => {
        search.flag = false
     
    })
    //输入响应，截流
    input.addEventListener('keyup', () => {
        console.log(search.lock)
        if (!search.lock) return
        search.lock = false
        setTimeout(async () => {
            //查询方法
            const query = search.inputData
            console.log(query)
            if(query) {
                const data = await webData.queryDocument(query)
                search.searchData = data
                search.lock = true
            }
        }, 3000);
    })
}

//获取目录
export const getContext = async (query: string) => {
    // storage.remove('context')
    const verify = verifyStorageData('context')
    if (verify) {
        const data = storage.get('context')
        console.log('获取缓存目录数据')
        const { classify } = data.value
        // console.log(classify)
        if (query !== classify) {
            console.log('下载目录数据')
            const data = await webData.getContextData(query)
            // console.log(data)
            context.contextData = data
            storage.set('context', data)
        } else {
            context.contextData = data.value.context
            // console.log(data)

        }

    } else {
        console.log('下载目录数据')
        const data = await webData.getContextData(query)
        // console.log(data)
        context.contextData = data
        storage.set('context', data)
    }
}

//转换标题
export const changeName = (data: { route: string | string[] }) => {
    if (data.route === 'sleep') {
        return ref('失眠')
    } else if (data.route === 'xuanyun') {
        return ref('眩晕')
    } else {
        return ref('皮肤病')
    }
}


//日期转换
export function timePrase(number:number) {
    const date = new Date(number)
    return [date.getFullYear(), (date.getMonth() + 1).toString().padStart(2, '0'), date.getDate().toString().padStart(2, '0')].join('-') + ' ' + [date.getHours().toString().padStart(2, '0'), date.getMinutes().toString().padStart(2, '0')].join(':')
}

